NavigationStack with path
NavigationStack.path provides observable, programmatic control over the navigation stack. It allows direct manipulation of the navigation history using a bound observable array.
It enables:
- Programmatic navigation
- Multi-level stack control
- Returning to the root view
- Dynamic page resolution via
NavigationDestination
1. API Definition
2. Type and Semantics of path
path is an observable string array representing the current navigation stack.
Rules:
- Each
stringrepresents a unique page identifier - The array order defines the navigation order
- The last element is the currently visible page
- An empty array represents the root view
Examples:
Represents the root view
Represents navigation to page a
Represents navigation to page a, then to page b, with b as the active page
3. Basic Usage Example
4. How path Controls Navigation
4.1 path as the Single Source of Navigation State
When path is bound:
- The full navigation stack is determined exclusively by
path.value - UI navigation state stays fully synchronized with
path - Implicit push/pop navigation is replaced by explicit state control
4.2 Pushing Pages
System behavior:
- Pushes page
aonto the stack - Displays page
a
System behavior:
- Pushes page
a - Then pushes page
b - Displays page
b
4.3 Popping Pages and Returning to Root
System behavior:
- Clears the entire navigation stack
- Immediately returns to the root view
5. Relationship Between path and NavigationDestination
NavigationDestination dynamically renders destination views based on the current value of path:
Rules:
-
pageis always equal to the last element ofpath.value -
When
pathchanges:pageupdates automatically- The destination view re-renders automatically
Mapping result examples:
6. Controlling Navigation with Buttons
Navigate to page a:
Navigate to page b:
Navigate through multiple pages:
Return to the root view:
7. Synchronization with System Back Gestures
When the user navigates back using:
- The system back gesture
- The navigation bar back button
Then:
path.valueis automatically updated- The navigation stack and UI remain fully synchronized
- No manual back handling is required
8. Typical Use Cases
NavigationStack.path is suitable for:
- Deep linking
- Multi-step navigation flows
- Programmatic routing
- Script-driven navigation
- Navigation state restoration
- Wizard-style navigation
- Cross-page navigation control
9. Common Errors
9.1 Incorrect Initialization
Incorrect:
Correct:
9.2 Invalid Path Element Types
Incorrect:
Correct:
Currently, only string[] is supported as the navigation path type.
